﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Data;

namespace SdmxMl.Panels
{
    public partial class DlgCsvProperties : Form
    {
        public CsvProperties Properties { get; set; }

        public DlgCsvProperties(CsvProperties p)
        {
            InitializeComponent();
            Properties = p;
            BindToControls(Properties);

        }

        private void BindToControls(CsvProperties p)
        {
            textBoxDelimiter.Text = new string(p.Delimiter, 1);
            textBoxComment.Text = new string(p.CommentMarker, 1);
            textBoxQuote.Text = new string(p.Quote, 1);

            //Chunk
            numericUpDownHuge.Value = p.Huge_MB;
            numericUpDownChunk.Value = p.ChunkSize_KB;
        }

        private void BindToProperties()
        {
            Properties.Delimiter = textBoxDelimiter.Text.Length == 0 ? ';' : textBoxDelimiter.Text[0];
            Properties.CommentMarker = textBoxComment.Text.Length == 0 ? '#' : textBoxComment.Text[0];
            Properties.Quote = textBoxQuote.Text.Length == 0 ? '"' : textBoxQuote.Text[0];

            //Chunk
            Properties.Huge_MB = (int)numericUpDownHuge.Value;
            Properties.ChunkSize_KB  = (int)numericUpDownChunk.Value;
        }

        #region Buttons Event

        private void buttonLoadMap_Click(object sender, EventArgs e)
        {
            try
            {
                openDialog.Filter = "CSV config files|*.cfg|All Files|*.*";
                if (openDialog.ShowDialog() != DialogResult.OK) return;
                {
                    CsvProperties m = new CsvProperties();
                    m = m.XmlRead(openDialog.FileName);
                    Properties = m;
                    BindToControls(Properties);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to load config file: \n" + ex.Message);
            }


        }

        private void buttonSaveMap_Click(object sender, EventArgs e)
        {
            try
            {
                BindToProperties();

                saveFileDialog.Filter = "CSV config files|*.cfg|All Files|*.*";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    Properties.XmlWrite(saveFileDialog.FileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to save config file: \n" + ex.Message);
            }


        }

        private void buttonResetCfg_Click(object sender, EventArgs e)
        {
            Properties = new CsvProperties();
            BindToControls(Properties);

        }
        #endregion

        private void DlgCsvProperties_FormClosing(object sender, FormClosingEventArgs e)
        {
            BindToProperties();
        }
    }
}
